package by.qizer.supercamp.dao;

import by.qizer.supercamp.domain.Inside;
import by.qizer.supercamp.domain.Journal;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Pribytkoyski Alexey
 *         User: qizer
 *         Date: 08.04.12
 */
@Repository
public class JournalDAOImpl implements JournalDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public void addJournal(Inside inside) {
        System.out.println("\n\n\t Invite to addJournal");
        Journal journal = new Journal();
        System.out.println("\n\n\t create journal");
        journal.setAutoId(inside.getAutoId());
        System.out.println("\n\n\t get auto");
        journal.setInGuardId(inside.getGuardId());
        journal.setOutGuardId(inside.getGuardId());
        System.out.println("\n\n\t getGuard");
        journal.setInTime(inside.getInTime());
        System.out.println("\n\n\t get time");
//        journal.setOthers(inside.getOther() + " / " + journal.getOthers());

        System.out.println(journal.getAutoId().getId());
        System.out.println(journal.getAutoId().getMark());
        System.out.println(journal.getAutoId().getRegNumber());
        System.out.println(journal.getAutoId().getOwnerId().getId());
        System.out.println(journal.getAutoId().getOwnerId().getLastName());
        System.out.println(journal.getAutoId().getOwnerId().getFirstName());
        System.out.println(journal.getAutoId().getOwnerId().getMiddleName());
        System.out.println(journal.getAutoId().getOwnerId().getPhone());
        System.out.println(journal.getAutoId().getOwnerId().getAddress());
        System.out.println(journal.getInGuardId().getId());
        System.out.println(journal.getInGuardId().getLastName());
        System.out.println(journal.getInGuardId().getFirstName());
        System.out.println(journal.getInGuardId().getMiddleName());
        System.out.println(journal.getInGuardId().getPhone());
        System.out.println(journal.getOutGuardId());
        System.out.println(journal.getInTime());
        System.out.println(journal.getOutTime());
        System.out.println(journal.getOthers());
        sessionFactory.getCurrentSession().save(journal);
    }

    @SuppressWarnings("unchecked")
    public List<Journal> listJournal() {
        return sessionFactory.getCurrentSession().createQuery("from Journal").list();
    }

    public void removeJournal(Integer id) {
        Journal journal = (Journal) sessionFactory.getCurrentSession().load(Journal.class, id);
        if (journal != null) {
            sessionFactory.getCurrentSession().delete(journal);
        }
    }
}
